// require jquery
// http://stackoverflow.com/questions/1184624/convert-form-data-to-js-object-with-jquery

define([
    'jquery',
    'js/config'
], function($, config) {

    // add jquery form 
    if (!$.fn.serializeObject) {
        $.fn.serializeObject = function() {
            var o = {};
            var a = this.serializeArray();
            $.each(a, function() {
                if (o[this.name] !== undefined) {
                    if (!o[this.name].push) {
                        o[this.name] = [o[this.name]];
                    }
                    o[this.name].push(this.value || '');
                } else {
                    o[this.name] = this.value || '';
                }
            });
            return o;
        };
    }


    var myUtils = {};

    myUtils.ajaxPostJson = function(url, json, cb) {
        $.ajax({
            url: url,
            type: "POST",
            headers: {
                "Content-Type": "application/json;charset=UTF-8"
            },
            data: JSON.stringify(json),
            success: function(res) {
                cb(null, res);
            },
            error: function(xhr) {
                cb(xhr.responseText);
            }
        });
    };

    myUtils.ajaxPutJson = function(url, json, cb) {
        $.ajax({
            url: url,
            type: "PUT",
            headers: {
                "Content-Type": "application/json;charset=UTF-8"
            },
            data: JSON.stringify(json),
            success: function(res) {
                cb(null, res);
            },
            error: function(xhr) {
                cb(xhr.responseText);
            }
        });
    };

    myUtils.ajaxGet = function(url, cb) {
        $.ajax({
            url: url,
            type: "GET",
            success: function(res) {
                cb(null, res);
            },
            error: function(xhr) {
                cb(xhr.responseText);
            }
        });
    };

    var forumPrefixUrl = config.getHost() + "/forum/v1"; // api
    // 登录接口
    myUtils.login = function(formData, cb) {
        var url = forumPrefixUrl + "/user/login";
        myUtils.ajaxPostJson(url, formData, cb);
    };

    myUtils.isLogined = function(cb) {
        var url = forumPrefixUrl + "/user/islogin";
        myUtils.ajaxGet(url, cb);
    };

    // register 接口
    myUtils.register = function(formData, cb) {
        var url = forumPrefixUrl + "/user/";
        myUtils.ajaxPostJson(url, formData, cb);
    };

    // register 接口
    myUtils.postThread = function(formData, cb) {
        var url = forumPrefixUrl + "/thread/";
        myUtils.ajaxPostJson(url, formData, cb);
    };

    var __THREAD_PAGE_SIZE = 10;
    // page, pageSize = 10
    myUtils.getThreadByPage = function(page, cb) {
        var url = forumPrefixUrl + "/thread/list?page=" + page + "&size=" + __THREAD_PAGE_SIZE;
        myUtils.ajaxGet(url, cb);
    };

    myUtils.getUrlThreadList = function(page) {
        return forumPrefixUrl + "/thread/list?page=" + page + "&size=" + __THREAD_PAGE_SIZE;
    };

    myUtils.getUrlThreadItem = function(_id) {
        if (!_id) return forumPrefixUrl + "/thread";
        return forumPrefixUrl + "/thread/" + _id;
    };


    myUtils.getAllPermissions = function(cb) {
        var url = forumPrefixUrl + "/permission/list";
        myUtils.ajaxGet(url, cb);
    };

    myUtils.getAllRoles = function(cb) {
        var url = forumPrefixUrl + "/role/list";
        myUtils.ajaxGet(url, cb);
    };

    myUtils.getRole = function(id, cb) {
        var url = forumPrefixUrl + "/role/" + id;
        myUtils.ajaxGet(url, cb);
    };

    myUtils.addRole = function(json, cb) {
        var url = forumPrefixUrl + "/role/";
        myUtils.ajaxPostJson(url, json, cb);
    };

    myUtils.editRole = function(json, cb) {
        var url = forumPrefixUrl + "/role/";
        myUtils.ajaxPutJson(url, json, cb);
    };

    myUtils.getOrgnizationTree = function(cb) {
        var url = forumPrefixUrl + "/orgnization/tree";
        myUtils.ajaxGet(url, cb);
    };

    return myUtils;
});